package com.example.lc.mythreedemo;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.io.InputStream;

/**
 * 图形工具类
 */
public class Graphic {
    public final static int TAG_WIDTH = 0;
    public final static int TAG_HEIGHT = 1;
    public final static int IMAGE_TURN_LEFT = 2;
    public final static int IMAGE_TURN_RIGHT = 3;

    /**
     * 根据传入类型获取屏幕高宽(包含状态栏)
     *
     * @param tagWH Graphic.TAG_WIDTH  /  Graphic.TAG_HEIGHT
     * @return
     */
    public static int getScreenWH(int tagWH) {
        WindowManager wm = (WindowManager) ContextHolder.getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        switch (tagWH) {
            case TAG_WIDTH:
                return metrics.widthPixels;
            case TAG_HEIGHT:
                return metrics.heightPixels;
            default:
                return 0;
        }
    }

    /**
     * GridView中item按比例设置宽与高
     *
     * @param widthScale  图片比例(宽)
     * @param heightScale 图片比例(高)
     * @param numColumns  列数
     * @param exclusiveOf 不计算在内的宽度例如:layout_marginLeft / layout_marginRight / verticalSpacing 等,单位为dp
     * @param v           控件view
     * @param viewGroup 父布局类型 目前只写了两种 new LinearLayout(context) 与 new RelativeLayout(context)
     */
    public static void setViewWH(int widthScale, int heightScale, int numColumns, int exclusiveOf, View v, ViewGroup viewGroup) {
        // item所占据的总宽度
        int itemWH = Graphic.getScreenWH(Graphic.TAG_WIDTH) - Graphic.dp2px(ContextHolder.getContext(), exclusiveOf);// 40item的间隔
        // 图片固定比例为16:9
        int width = itemWH / numColumns;
        int height = width / widthScale * heightScale;
        if (viewGroup instanceof RelativeLayout) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
            v.setLayoutParams(params);
        } else if (viewGroup instanceof LinearLayout) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
            v.setLayoutParams(params);
        }
    }

    /**
     * 获取listView或gridview的item的宽或高
     *
     * @param activity  一定为activity
     * @param columnNum 要划分为多少块
     * @param Spacing   每一块之间的间距（单位为dp)
     * @param marginLR  左右的间距（单位dp）
     * @return
     */
    public static int getListItemWH(Activity activity, int columnNum,
                                    int Spacing, int marginLR) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int marginLR_PX = Graphic.dp2px(activity, marginLR) * 2;// 左右的间距总和
        int padingInsideTotal_PX = Graphic.dp2px(activity, Spacing)
                * (columnNum - 1);// 内部间距总和
        int itemWHTotal = metrics.widthPixels - marginLR_PX
                - padingInsideTotal_PX;// item总宽
        return itemWHTotal / columnNum;// 每一个item的宽度或高度
    }

    /**
     * 设置view宽高(线性布局linearLayout)
     *
     * @param wh 控件的宽高,单位px
     * @param v  控件view
     */
    public static void setViewWHLine(int wh, View v) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(wh, wh);
        v.setLayoutParams(params);
    }

    /**
     * 设置View宽高(相对布局relativeLayout)
     *
     * @param wh 控件的宽高,单位px
     * @param v  控件
     */
    public static void setViewWHRela(int wh, View v) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                wh, wh);
        v.setLayoutParams(params);
    }

    /**
     * 设置view宽高(线性布局linearLayout)
     *
     * @param w 控件宽 ,单位px
     * @param h 控件高,单位px
     * @param v 控件view
     */
    public static void setViewWHLine(int w, int h, View v) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(w, h);
        v.setLayoutParams(params);
    }

    /**
     * 设置view宽高(相对布局relativeLayout)
     *
     * @param w 控件宽,单位px
     * @param h 控件高,单位px
     * @param v 控件view
     */
    public static void setViewWHRela(int w, int h, View v) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(w,
                h);
        v.setLayoutParams(params);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dp(Context context, float pxValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        if (scale <= 0)
            return (int) pxValue;
        return (int) (pxValue / scale + 0.5f);
    }


    /**
     * 同dp2px
     *
     * @param context 上下文
     * @param dip     值
     * @return 像素
     */
    public static int dipToPixels(Context context, int dip) {
        Resources r = context.getResources();
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip,
                r.getDisplayMetrics());
        return (int) px;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     *
     * @param context
     * @param dpValue
     * @return
     */
    public static int dp2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        if (scale <= 0)
            return (int) dpValue;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2sp(Context context, float pxValue) {
        float scale = context.getResources().getDisplayMetrics().scaledDensity;
        if (scale <= 0)
            return (int) pxValue;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param context
     * @param spValue
     * @return
     */
    @Deprecated
    // 不太靠谱
    public static int sp2px(Context context, float spValue) {
        float scale = context.getResources().getDisplayMetrics().scaledDensity;
        if (scale <= 0)
            return (int) spValue;
        return (int) (spValue * scale + 0.5f);
    }

    /**
     * 解码图片流，据说可以避免内存溢出：bitmap size exceeds VM budget
     *
     * @param picStream
     * @return
     */
    public static Bitmap decodeStream(InputStream picStream) {
        Bitmap bitmap = null;
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = 4;
        bitmap = BitmapFactory.decodeStream(picStream);
        return bitmap;
    }
}
